/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2019年1月7日
 */

package com.massyframework.beanskin.spring.beancontext;

import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;

import com.massyframework.beanskin.assembly.Assembly;

import java.util.List;

/**
 * {@link AssemblyPropertySource}, 提供对装配件初始化参数的{@link PropertySource}封装
 */
public final class AssemblyPropertySource extends EnumerablePropertySource<Assembly> {

	private static final String GROUPID      = ".groupId";
    private static final String MODULEID     = ".moduleId";
    private static final String VERSION      = ".version";

    /**
     * 构造方法
     * @param source {@link InitParameters}, 初始化参数
     */
    public AssemblyPropertySource(Assembly source) {
        super("assembly", source);
    }

    @Override
    public boolean containsProperty(String name) {
        return this.getSource().getConfig().getParameter(name) != null;
    }

    @Override
    public Object getProperty(String s) {
        if (GROUPID.equals(s)) {
            return this.getSource().getGroupId();
        }
        
        if (MODULEID.equals(s)) {
        	return this.getSource().getModuleId();
        }

        if (VERSION.equals(s)){
            return this.getSource().getVersion();
        }


        return this.getSource().getConfig().getParameter(s);
    }

    @Override
    public String[] getPropertyNames() {
        List<String> names = this.getSource().getConfig().getParameterNames();
        names.add(GROUPID);
        names.add(MODULEID);
        names.add(VERSION);
        return names.toArray(new String[names.size()]);
    }
}
